// ------ 页码配置 ------
let form = layui.form;
let laypage = layui.laypage;
let data = {
    pagenum:1,
    pagesize:2,
}
let t; // 用户总数
function renderPage(t){
    laypage.render({
        elem: 'page', //注意，这里的 test1 是 ID，不用加 # 号
        count: t, //数据总数，从服务端得到
        limit: data.pagesize,
        limits: [2,3,5,10],
        curr: data.pagenum,
        groups: 3,
        layout: ["prev","page","next","skip","count","limit"],
        jump: function(obj, first){
            //obj包含了当前分页的所有参数，比如：
            //console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
            //console.log(obj.limit); //得到每页显示的条数
            //首次不执行
            if(!first){
              //do something
              data.pagenum = obj.curr;
              data.pagesize = obj.limit;
              renderUser();
            }
        }
    });
}
// renderPage();


// ------ 获取用户列表 ------
function renderUser(){
    $.ajax({
        url:"admin/users",
        data:data,
        success: function(res){
            // console.log(res);
            let html = template("tpl-user",res);
            $("tbody").html(html);
            t = res.total;
            renderPage(t);
        }
    })
}
renderUser();


// ------ 删除用户 ------
$("tbody").on("click","button:contains('删除')",function(){
    let id = $(this).data("id");
    layer.confirm('是否确认删除？', {icon: 3, title:'提示'}, function(index){
        $.ajax({
            type:"delete",
            url:"admin/users/" + id,
            success:function(res){
                layer.msg(res.message);
                renderUser();
            }
        })
        layer.close(index);
    });
    
})


// ------ 编辑用户 ------
let index;
$("tbody").on("click","button:contains('编辑')",function(){
    index = layer.open({
        type: 1, 
        title:"编辑用户",
        area: ["500px", "300px"],
        content: $("#tpl-edit").html()
    });
    let editId = $(this).data("id");
    // 获取用户信息
    $.ajax({
        url:"admin/users/" + editId,
        success: function(res){
            form.val("edit", {
                "username": res.data.username,
                "nickname": res.data.nickname,
                "email":res.data.email,
                "id":res.data.id,
            });
        }   
    })
})
// 提交用户信息
$("body").on("submit",".edituser",function(e) {
    e.preventDefault();
    let editData = $(this).serialize();
    $.ajax({
        type:"put",
        url:"admin/users",
        data:editData,
        success:function(res){
            layer.msg(res.message);
            layer.close(index);
            renderUser();
        }

    })
})


// ------ 重置密码 ------
let pwdId;
$("tbody").on("click","button:contains('重置密码')",function(){
    layer.open({
        type: 1, 
        title:"重置密码",
        area: ["500px", "250px"],
        content: $("#tpl-pwd").html()
      });
      pwdId = $(this).data("id");
})
$("body").on("submit",".repwd",function(e){
    e.preventDefault();
    let newpwd = $(".newpwd").val();
    $.ajax({
        type:"put",
        url:"admin/users/" + pwdId,
        data:{ password: newpwd },
        success:function(res){
            layer.msg(res.message);
            if(res.status === 0){
                localStorage.removeItem("mytoken");
                setTimeout("top.location.href='/admin/login1.html'", 1000);
            }
        }
    })
})


// ------ 表单验证 ------
form.verify({
    email:[
        /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
        '邮箱格式不正确'
    ],
    pass: [
      /^[\S]{6,12}$/,
      '密码必须6到12位，且不能出现空格'
    ],
    same: function(value, item){ //value：表单的值、item：表单的DOM对象
        let val = $(".newpwd").val();
        if(value !== val) {
        return '两次输入的密码不一致';
        }
    },
  });